﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using NOAH.CMS.Repository;

using ASP;
public partial class admin_Modules_Common_Config_AddOrEdit : System.Web.UI.Page
{
    public int configID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.Request.QueryString["id"] != null)
        {
            int.TryParse(Request.QueryString["id"], out configID);
        }
        if (!X.IsAjaxRequest)
        {
            ShowDetail();
        }
    }

    private void ShowDetail()
    {
        ConfigRepository configRepository = new ConfigRepository();
        Config config = new Config();
        if (configID > 0)
        {
            config = configRepository.Get(configID);
            txtKey.Text = config.Key;
            txtDescription.Text = config.Description;
           fckHtmlEditor.Text = config.Value;
            //fckHtmlEditor.Value = config.Value;
           // ResourceManager1.RegisterClientScriptBlock("Loadtext", "setFckText('xx');");
            hdfPlainText.Text = config.Value;
            txtKey.Disabled = true;
        }

    }


    public void OnSave(object sender, DirectEventArgs e)
    {


        try
        {

            ConfigRepository configRepository = new ConfigRepository();
            Config config = new Config();
            if (configID > 0)
            {
                config = configRepository.Get(configID);
            }

            config.Key = txtKey.Text;
            config.Description = txtDescription.Text;
            config.Value = e.ExtraParams["fckParameter"].ToString();

            config.Value = HttpUtility.HtmlDecode(config.Value);

            if (configID == 0)
            {
                configRepository.Add(config);
            }
            configRepository.SubmitChanges();
            new NOAH.CMS.Cache.ConfigCacheLayer().InvalidateCache();

            ExtNet.Msg.Hide();
            Util.ShowMessage(Constants.UPDATE_SUCCESS);
            BasePage.RegisterCallBackFromChild(this.ResourceManager1, string.Empty);
            if (configID > 0)
            {
                BasePage.RegisterCloseWindow(this.ResourceManager1);
            }
        }
        catch (Exception ex)
        {
            //Util.ShowMessage(Constants.UPDATE_FAILURE);
            Util.ShowMessage("", ex.Message + " : " + ex.StackTrace, 300, 200);
        }
    }

}